package com.ware.validator;

import com.jfinal.core.Controller;
import com.ware.controller.BaseController;
import com.ware.util.Validator;

public class ConnectionValidator extends AjaxValidator {

	@Override
	protected void handleError(Controller c) {
		BaseController controller = (BaseController)c;
		controller.renderAjaxError(getLastMessage());
	}

	@Override
	protected void validate(Controller c) {
		this.setShortCircuit(true);
		try {
			if(!Validator.validateRegex(c.getPara("comInfo.company_name"), "[\u4e00-\u9fa5\\dA-Za-z_]{1,20}")){
				this.addError("info", "保存失败：公司名称不正确！");
			}
			if(!Validator.validateString(c.getPara("comInfo.addr"), 0, 50)){
				this.addError("info", "保存失败：公司地址不正确！");
			}
			if(!(Validator.validateMobile(c.getPara("comInfo.tell")) || Validator.validateRegex(c.getPara("storeInfo.tel"), "^(0\\d{2,3}\\-)?([2-9]\\d{6,7})+(\\-\\d{1,4})?$"))){
				this.addError("info", "保存失败：联系电话不正确！");
			}
			if(!Validator.validateMobile(c.getPara("comInfo.phone"))){
				this.addError("info", "保存失败：手机填写不正确！");
			}
			if(c.getPara("comInfo.fax","").length()>30){
				this.addError("info", "保存失败：传真填写不正确！");
			}
			if(c.getPara("comInfo.remark","").length()>200){
				this.addError("info", "保存失败：备注字数超长！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e.getMessage() != null) {
				this.addError("info", "保存失败：" + e.getMessage());
			}
		}

	}

}
